package com.cloud.controller;

import com.cloud.feign.ItemFeignService;
import com.cloud.service.IOrderService;
import io.seata.core.exception.TransactionException;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @Author wenbo
 * @Date 2022/12/13 11:02
 **/
@RestController
@RequestMapping("/order")
public class OrderController {


    @Resource
    private ItemFeignService itemFeignService;


    @Resource
    private IOrderService iOrderService;


    @GetMapping("/doOrder")
    @GlobalTransactional
    public String doOrder() {
        //1.下单
        String info = itemFeignService.doOrder();
        System.out.println("下单成功,订单信息为："+info);
        //3.模拟返回成功信息
        return info;
    }

    @GetMapping("/getOrder")
    public String getOrder() {
        //1.模拟远程调用获取到到商品信息
        String info = itemFeignService.getItemInfo("1");
        System.out.println("获取订单信息为："+info);
        //3.模拟返回成功信息
        return info;
    }

    @GetMapping("/addOrder")
    public String addOrder() {
        String str = itemFeignService.decStock(1L,1L);
        return str;
    }

    @GetMapping("/addOrder2")
    @Trace
    @Tag(key = "addOrder2",value = "returnedObj")
    @Tag(key = "addOrder2",value = "arg[0]")
    @Tag(key = "addOrder2",value = "arg[1]")
    public String addOrder2() throws TransactionException, InterruptedException {
        //TimeUnit.SECONDS.sleep(2);
        String str = iOrderService.createOrder(2L,2L);
        return str;
    }

    @GetMapping("/header")
    public String header(@RequestHeader("X-Request-color") String color) throws InterruptedException {
        System.err.println(color);
        TimeUnit.SECONDS.sleep(2);
        return color;
    }
}
